﻿package baseitems
{
	import com.bit101.components.Label;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.events.MouseEvent;
	import property.ItemType
    import baseitems.Base;

    public class Item extends Base
	{
		//create a new Rectangle to constrain the drag 
		//(used to enforce minimum width and height of 30 -- max values are irrelevant)
		private var rect:Rectangle = new Rectangle(20, 20, 3000000, 3000000);
		
		private var nub:Sprite;
		private var nameTF:Label;
		private var center:Sprite;
		public function Item(id:int, type:String)
		{
			super(id, type);
			init();
		}
		
		private function init():void
		{
			this.alpha = 0.7;

			nub = new Sprite();
			nub.graphics.lineStyle(1);
			nub.graphics.beginFill(0x000000);
			nub.graphics.drawRect(0, 0, -10, -10);
			nub.graphics.endFill();
			nub.x = 50;
			nub.y = 50;
			container.addChild(nub);
			
			center = new Sprite();
			center.graphics.beginFill(0x333333);
			center.graphics.drawCircle(0, 0, 3);
			center.graphics.endFill();
			addChild(center);
			
			nameTF = new Label(container, 10, 10, name);
			
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true);
			nub.addEventListener(MouseEvent.MOUSE_DOWN, onNubMouseDown, false, 0, true);
			
			nub.buttonMode = true;
			
			onStageMouseMoveFromNub(null);
		}
		
		//called by the MouseEvent.MOUSE_DOWN event on this, the SnapshotRectangle instance
		private function onMouseDown(evt:MouseEvent):void
		{
			//start dragging this (the SnapshotRectangle instance), without snapping mouse to center
			//var rect:Rectangle = new Rectangle(0, 0, 350 - this.width, 350-this.height - 19);
			startDrag(false);
			//listen for the mouseUp event on the stage
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp, false, 0, true);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMoveFromNub, false, 0, true);
		}
		
		//called by the MouseEvent.MOUSE_DOWN event on the nub
		private function onNubMouseDown(event:MouseEvent):void
		{
			//stop the event from propagating (so this, the SnapshotRectangle instance, does not hear it)
			event.stopImmediatePropagation();
			
			//start dragging the nub, without snapping mouse to center, constraining to _rect
			nub.startDrag(false, rect);
			//add _onStageMouseUpFromNub as a callback to the mouseUp event on the stage
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUpFromNub, false, 0, true);
			//add _onStageMouseMoveFromNub as a callback to the mouseMove event on the stage
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMoveFromNub, false, 0, true);
		}
		
		//called by the MouseEvent.MOUSE_UP event on the stage, after _onMouseDown has been run
		//to stop the drag move operation
		private function onStageMouseUp(event:MouseEvent):void
		{
			//stop dragging (this, the SnapshotRectangle instance)
			stopDrag();
			//remove _onStageMouseUp as a callback from the mouseUp event on the stage
			stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMoveFromNub);
		}
		
		//called by the MouseEvent.MOUSE_UP event on the stage, after _onNubMouseDown has been run
		//to stop the drag resize operation
		private function onStageMouseUpFromNub(event:MouseEvent):void
		{
			//stop dragging (nub)
			stopDrag();
			//remove _onStageMouseUpFromNub as a callback from the mouseUp event on the stage
			stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUpFromNub);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMoveFromNub);
		}
		
		//called by the MouseEvent.MOUSE_MOVE event on the stage, after _onNubMouseDown has been run
		//to update the drag resizing
		private function onStageMouseMoveFromNub(event:MouseEvent):void
		{
			//force nub's x and y to integers
			nub.x = Math.round(nub.x);
			nub.y = Math.round(nub.y);
			
			//match width and height of bg to x and y of nub, respectively
			container.graphics.clear();
			container.graphics.lineStyle(1);
			container.graphics.beginFill(0x0CCCCCC);
			container.graphics.drawRect(0, 0, nub.x, nub.y);
			container.graphics.endFill();
			
			//reflect the accurate dimensions in the tf_dims textfield
			//so user knows how big an image he'll create by snapping
			this.x = Math.round(this.x);
			this.y = Math.round(this.y);
		
			nameTF.x = width / 2 - nameTF.width/2;
			nameTF.y = height / 2 - nameTF.height / 2;
			//nameTF.visible = ((nub.x > nameTF.width ))
		}
		
		override public function set width(value:Number) :void {
			nub.x = Math.round(value);
			onStageMouseMoveFromNub(null);
		}
		
		override public function set height(value:Number) :void {
			nub.y = Math.round(value);
			onStageMouseMoveFromNub(null);
		}
		
		// берем положение nub (один хер он в углу) ибо если брать
		// настоящую ширину - то она на 1 больше из за линий обводки
		override public function get width():Number {
			return nub.x;
		}
		
		override public function get height():Number {
			return nub.y;
		}
		
		override public function set selected(value:Boolean):void {
			super.selected = value;
			center.visible = value;
			nub.visible = value;
			nameTF.visible = value;
			nameTF.text = name;
			// нода и леер полупрозрачные в тот момент когда они не выделенные, збс же не мешают
			if (!value && (type==ItemType.NODE || type==ItemType.LAYER)) {
				container.alpha = 0.5;
			} else {
				container.alpha = 1;
			}
		}
	}
}
